import _ from 'lodash';
import fasNumeral from './fasNumeral';
import { DataParse } from './format';

export let FormatFunctions = {
  Number: (cellInfo, col) => {
    let returnValue = cellInfo.value;

    let formatString = '0';
    if (col.dataFormat.ThousandsSeparator) {
      formatString = `,${formatString}`;
    }

    if (false === _.isNil(col.dataFormat.DecimalDigit)) {
      let decimalDigit = col.dataFormat.DecimalDigit;
      // returnValue = _.round(returnValue, decimalDigit)
      formatString = `${formatString}.${_.repeat('0', decimalDigit)}`;
    }

    returnValue = fasNumeral(cellInfo.value, formatString);

    return returnValue;
  },
  Currency: (cellInfo, col) => {
    let returnValue = cellInfo.value;
    returnValue = FormatFunctions.Number(cellInfo, col);
    let currencySymbol = col.dataFormat.CurrencySymbol || '';
    if (currencySymbol === 'auto') {
      currencySymbol = '￥';
    }
    returnValue = currencySymbol + returnValue;
    return returnValue;
  },
  Percent: (cellInfo, col) => {
    let returnValue = cellInfo.value;
    returnValue = returnValue * 100;
    returnValue = FormatFunctions.Number({ value: returnValue }, col);
    returnValue = `${returnValue}%`;
    return returnValue;
  },
  DateTime(cellInfo, col) {
    let returnValue = cellInfo.value;
    if (returnValue == null) {
      return returnValue;
    }
    if (returnValue && returnValue instanceof Date) {
      returnValue = new DataParse(returnValue).DateTime(
        col.dataFormat.DateFormat,
      ).data;
    } else {
      let dataValue = new Date(returnValue);
      let fffff = new DataParse(dataValue).DateTime('yyyy-MM-dd HH:mm:ss').data;
      returnValue = new DataParse(dataValue).DateTime(
        col.dataFormat.DateFormat,
      ).data;

      try {
        let fff = new Date(cellInfo.value);
        let BBB = new Date(fffff);

        if (fff.valueOf() != BBB.valueOf()) {
          returnValue = cellInfo.value;
        }
      } catch (error) {
        returnValue = cellInfo.value;
      }

      // return fff==BBB ? returnValue :
    }
    return returnValue;
  },
  Discount(cellInfo, col) {
    let returnValue = cellInfo.value;
    if (returnValue && window.Funcs && Funcs.GetDetailDiscountRate) {
      let fixedRule = _.get(col, 'dataFormat.DiscountFixedRule', '1');
      let dr = Funcs.GetDetailDiscountRate(returnValue, fixedRule);
      if (dr) {
        returnValue = `${dr.prefix}${dr.value}${dr.suffix}`;
      }
    }
    return returnValue;
  },
};

export default FormatFunctions;
